/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SineControlPanel.java
 *
 * Created on 28 nov. 2009, 18:34:30
 */

package modbuspal.generator.sine;

import modbuspal.toolkit.NumericTextField;

/**
 * the control panel for the sine generator
 * @author nnovic
 */
public class SineControlPanel
extends javax.swing.JPanel
{
    private final SineGenerator sineGen;

    /** Creates new form SineControlPanel 
     * @param gen the sine generator whose parameters are being displayed by this component
     */
    public SineControlPanel(SineGenerator gen)
    {
        sineGen = gen;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        amplitudeTextField = new NumericTextField(sineGen.amplitude);
        jLabel2 = new javax.swing.JLabel();
        periodTextField = new NumericTextField(sineGen.period);
        catchupCheckBox = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        offsetTextField = new NumericTextField(sineGen.offset);

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Amplitude:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jLabel1, gridBagConstraints);

        amplitudeTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                amplitudeTextFieldFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(amplitudeTextField, gridBagConstraints);

        jLabel2.setText("Period:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jLabel2, gridBagConstraints);

        periodTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                periodTextFieldFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(periodTextField, gridBagConstraints);

        catchupCheckBox.setSelected(sineGen.catchup);
        catchupCheckBox.setText("Catch up");
        catchupCheckBox.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                catchupCheckBoxFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(catchupCheckBox, gridBagConstraints);

        jLabel3.setText("Offset:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jLabel3, gridBagConstraints);

        offsetTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                offsetTextFieldFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(offsetTextField, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void amplitudeTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_amplitudeTextFieldFocusLost
        
        double newAmp = ((NumericTextField)amplitudeTextField).getDouble();
        if( newAmp != 0 )
        {
            sineGen.amplitude = newAmp;
        }
        else
        {
            ((NumericTextField)amplitudeTextField).setValue(sineGen.amplitude);
        }

    }//GEN-LAST:event_amplitudeTextFieldFocusLost

    private void periodTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_periodTextFieldFocusLost
        
        double newP = ((NumericTextField)periodTextField).getDouble();
        if( newP != 0 )
        {
            sineGen.period = newP;
        }
        else
        {
            ((NumericTextField)periodTextField).setValue(sineGen.period);
        }
    }//GEN-LAST:event_periodTextFieldFocusLost

    private void catchupCheckBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_catchupCheckBoxFocusLost
        sineGen.catchup = catchupCheckBox.isSelected();
    }//GEN-LAST:event_catchupCheckBoxFocusLost

    private void offsetTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_offsetTextFieldFocusLost
        double newO = ((NumericTextField)offsetTextField).getDouble();
        sineGen.offset = newO;
    }//GEN-LAST:event_offsetTextFieldFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JTextField amplitudeTextField;
    javax.swing.JCheckBox catchupCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    javax.swing.JTextField offsetTextField;
    javax.swing.JTextField periodTextField;
    // End of variables declaration//GEN-END:variables

}
